/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;


import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;


/**
 * 
 */
public class CommentsSection extends PropertyViewSection {
	
	
	/**
	 * 
	 */
	private Table table;
	
	/**
	 * 
	 */
	private org.eclipse.swt.widgets.Label separator;
	
	/**
	 * 
	 */
	private Text body = null;
	
	/**
	 * 
	 */
	private Button addButton, removeButton;
	
	/**
	 * 
	 */
	private MouseListener addButtonListener, removeButtonListener;
	
	/**
	 * 
	 */
	private SelectionListener tableListener;
	
	/**
	 * 
	 */
	private FocusListener bodyFocusListener;

	/**
	 * 
	 * 
	 * @param tabbedPropertySheetPage 
	 * @param parent 
	 */
	@Override
	public void createControls(Composite parent,
            TabbedPropertySheetPage tabbedPropertySheetPage) {
		
        super.createControls(parent, tabbedPropertySheetPage);
        Composite composite = getWidgetFactory()
            .createFlatFormComposite(parent);
        FormData data;
        
        separator = getWidgetFactory().createSeparator(composite, SWT.VERTICAL);
        separator.setVisible(false);
        data = new FormData();
        data.left = new FormAttachment(30);
        data.top = new FormAttachment(0, 0);
        data.bottom = new FormAttachment(90);
        separator.setLayoutData(data);

        // List of comments
        table = getWidgetFactory().createTable(composite, SWT.SINGLE | SWT.V_SCROLL | SWT.BORDER);
        TableColumn listOfComments = new TableColumn(table, SWT.LEFT);
        listOfComments.setText("List of Comments");
        listOfComments.setWidth(200);
        table.setHeaderVisible(true);
        
        data = new FormData();
		data.left = new FormAttachment(0, 0);
		//data.right = new FormAttachment(addButton, -ITabbedPropertyConstants.HSPACE);
		data.right = new FormAttachment(separator,-100);
		data.bottom = new FormAttachment(100);
		data.top = new FormAttachment(0, 0);
		table.setLayoutData(data);
		
		table.addSelectionListener(tableListener = new SelectionListener() {
			
			public void widgetSelected(SelectionEvent e) {
				TableItem[] selected = table.getSelection();
				body.setText(((Comment)selected[0].getData()).getBody());
			}
			
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		
		
		// Add comment button
		
		addButton = getWidgetFactory().createButton(composite, "Add...", SWT.PUSH);
		addButton.setVisible(true);
		data = new FormData();
		data.right = new FormAttachment(separator, -10);
		data.left = new FormAttachment(table, 10);
		data.top = new FormAttachment(0, 0);
		addButton.setLayoutData(data);
		
		addButton.addMouseListener(addButtonListener = new MouseListener() {
			public void mouseDoubleClick(MouseEvent e) {
			}
			
			public void mouseDown(MouseEvent e) {
			}
			
			public void mouseUp(MouseEvent e) {
				if(getElement() != null) {
					Comment newComment = UMLFactory.eINSTANCE.createComment();
					newComment.setBody("New_Comment");
					newComment.getAnnotatedElements().add(getElement());
					getElement().getOwnedComments().add(newComment);
					refresh();
				}
			}
		});

		
		// Remove comment button
		
		removeButton = getWidgetFactory().createButton(composite, "Remove...", SWT.PUSH);
		removeButton.setVisible(true);
		data = new FormData();
		data.right = new FormAttachment(separator, -10);
		data.left = new FormAttachment(table, 10);
		data.top = new FormAttachment(addButton, 5);
		data.bottom = new FormAttachment(30);
		removeButton.setLayoutData(data);
		
		removeButton.addMouseListener(removeButtonListener = new MouseListener() {
			public void mouseDoubleClick(MouseEvent e) {
			}
			
			public void mouseDown(MouseEvent e) {
			}
			
			public void mouseUp(MouseEvent e) {
				if((getElement() != null) && (table.getSelection().length > 0)) {
					TableItem[] items = table.getSelection();
					getElement().getOwnedComments().remove(items[0].getData());
					refresh();
				}
			}
		});
		
		
		// Comments description
        body = getWidgetFactory().createText(composite, "", SWT.V_SCROLL); 
        body.setEditable(true);
        data = new FormData();
        data.left = new FormAttachment(separator, STANDARD_LABEL_WIDTH);
        data.right = new FormAttachment(100, 0);
        data.top = new FormAttachment(0, 0);
        data.bottom = new FormAttachment(90);
        body.setLayoutData(data);
        
        body.addFocusListener(bodyFocusListener = new FocusListener() {
    		public void focusGained(FocusEvent e){
    		}
    		public void focusLost(FocusEvent e){
    			if((table.getSelection() != null) && (table.getSelection().length > 0)) {
    				TableItem[] items = table.getSelection();
    				Comment comment = (Comment)items[0].getData();
    				comment.setBody(body.getText().trim());
    			}
    			refresh();
    		}
        });
        
        CLabel bodyLabel = getWidgetFactory().createCLabel(composite, "Body:"); //$NON-NLS-1$
        data = new FormData();
        data.right = new FormAttachment(body,
            -ITabbedPropertyConstants.HSPACE);
        data.top = new FormAttachment(body, 0, SWT.TOP);
        bodyLabel.setLayoutData(data);
	}
	

	/**
	 * 
	 */
	@Override
	public void refresh() {
		if( !table.isDisposed() && !body.isDisposed()){
			List<Comment> comments = getElement().getOwnedComments();
			table.removeAll();
			body.setText("");
			for(int i=0; i<comments.size(); i++){
				TableItem comment = new TableItem(table, SWT.NONE);

				Comment currentComment = comments.get(i);
				String commentId = "";

				// UMLObjectLabelProvider provider = new UMLObjectLabelProvider();
				commentId = PapyrusPlugin.UML_LABEL_PROVIDER.getText(currentComment, true);

				comment.setText(commentId);
				comment.setData(comments.get(i));
			}
		}
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	/**
	 * 
	 */
	public void dispose(){
		super.dispose();
		if(addButton!=null && !addButton.isDisposed())
			addButton.removeMouseListener(addButtonListener);
		if(removeButton!=null && !removeButton.isDisposed())
			removeButton.removeMouseListener(removeButtonListener);
		if(body!=null && !body.isDisposed())
			body.removeFocusListener(bodyFocusListener);
		if(table!=null && !table.isDisposed())
			table.removeSelectionListener(tableListener);
	}
   

}

